package com.ems.service.impl.system;

import com.ems.dao.system.NoticeDao;
import com.ems.pojo.system.SysNotice;
import com.ems.service.system.NoticeService;
import com.ems.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 伟伟
 * @Date: 2020/1/2
 * @Description: 描述
 * @Version: 1.0
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
   private NoticeDao noticeDao;

    @Override
    public void addNotice(SysNotice sysNotice) {
        noticeDao.addNotice(sysNotice);
    }

    @Override
    public void deleteNotice(List<SysNotice> list) {
       for(int i = 0;i < list.size();i ++){
           noticeDao.deleteNoticeById(list.get(i));
       }
    }

    @Override
    public void updateNoticeById(SysNotice sysNotice) {
        noticeDao.updateNoticeById(sysNotice);
    }

    @Override
    public List<SysNotice> findNoticeByPage(Map<String, Object> map) {
        List<SysNotice> list = noticeDao.findNoticeByPage(map);

        //获取当前的时间
        long time = new Date().getTime();
        SysNotice sysNotice = null;
        for(int i = 0;i < list.size();i ++){
            sysNotice = list.get(i);
            if(Tools.getMillisecon(sysNotice.getN_start_time()) > time){  //未开始
                sysNotice.setStatus(0);
            }else if(time >= Tools.getMillisecon(sysNotice.getN_start_time()) && time <= Tools.getMillisecon(sysNotice.getN_end_time())){ //进行中
                sysNotice.setStatus(1);
            }else{ //已结束
                sysNotice.setStatus(-1);
            }
        }

        return list;
    }

    @Override
    public List<SysNotice> queryNoticeNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        List<SysNotice> list = noticeDao.queryNoticeNowTime(nowTime);
        return list;
    }

    @Override
    public void deleteUnStartNotice() {
        String time = Tools.getTime();
        noticeDao.deleteUnStartNotice(time);
    }

    @Override
    public void deleteStretedNotice() {
        String time = Tools.getTime();
        noticeDao.deleteStretedNotice(time);
    }
}
